package it.win4buy.presentations
{
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    import flash.ui.MouseCursor;

    import it.win4buy.entities.PlaceHolderVO;
    import it.win4buy.events.NavigationEvent;
    import it.win4buy.events.PlaceHolderEvent;
    import it.win4buy.events.UtilityEvent;
    import it.win4buy.models.ApplicationModel;
    import it.win4buy.utility.ApplicationConstants;
    import it.win4buy.utility.LoggerUtilty;
    import it.win4buy.views.PlaceHolderView;

    import mx.collections.ArrayCollection;
    import mx.controls.DataGrid;
    import mx.controls.dataGridClasses.DataGridBase;
    import mx.controls.dataGridClasses.DataGridColumn;
    import mx.controls.dataGridClasses.DataGridItemRenderer;
    import mx.controls.dataGridClasses.DataGridLockedRowContentHolder;
    import mx.events.DataGridEvent;
    import mx.events.DataGridEventReason;
    import mx.events.ListEvent;
    import mx.managers.CursorManager;
    import mx.olap.aggregators.CountAggregator;
    import mx.resources.ResourceManager;
    import mx.skins.halo.DataGridColumnDropIndicator;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class PlaceHolderViewPM
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        [Inject]
        public var applicationModel:ApplicationModel;

        [Inject]
        public var log:LoggerUtilty;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------

        /**
         * PlaceHolder's List
         */
        private var _placeHolderList:ArrayCollection;



        /**
         * [PlaceHolderInfo Panel] PlaceHolder's name
         */
        private var _infoPlaceHolder_labelName:String="";


        /**
         * [PlaceHolderInfo Panel] PlaceHolder's Price
         */
        private var _infoPlaceHolder_price:String="";


        /**
         * [PlaceHolderInfo Panel] PlaceHolder's ListAwards
         */
        private var _infoPlaceHolder_listAdwards:Object;


        /**
         * PlaceHolder selected.
         */
        private var _selectedIndex:int=-1;


        /**
         *
         * PlaceHolder currentState.
         *
         */
        private var _curretViewState:String="";


        /**
         * Used to enable the PLaceHolder View.<br>
         * this is used when showing the Alert state.
         */
        private var _enablePlaceHolder:Boolean;




        /**
         * Used to enable the Button btRegistration.
         */
        private var _enableBtRegistration:Boolean;


        /**
         *Used to make visible the Button btRegistration.
         */
        private var _visibleBtRegistration:Boolean=true;

        /**
         *Message showed in the AlertComponent.<br>
         * @return
         *
         */
        private var _alertMessage:String="";


        /**
         *Title of the alert Panel in the alert State.
         */
        private var _alertTitle:String="";

        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------
        [Bindable]
        public function get alertTitle():String
        {
            return _alertTitle;
        }

        public function set alertTitle(value:String):void
        {
            _alertTitle=value;
        }

        [Bindable]
        /**
         *Message showed in the AlertComponent.<br>
         * @return
         *
         */
        public function get alertMessage():String
        {
            return _alertMessage;
        }

        public function set alertMessage(value:String):void
        {
            _alertMessage=value;
        }

        [Bindable]
        public function get visibleBtRegistration():Boolean
        {
            return _visibleBtRegistration;
        }

        public function set visibleBtRegistration(value:Boolean):void
        {
            _visibleBtRegistration=value;
        }

        [Bindable]
        /**
         * Used to enable the Button btRegistration.<br>
         * @see PlaceHolderView.mxml
         */
        public function get enableBtRegistration():Boolean
        {
            return _enableBtRegistration;
        }

        public function set enableBtRegistration(value:Boolean):void
        {
            _enableBtRegistration=value;
        }


        [Bindable]
        /**
         * Used to enable the PLaceHolder View.<br>
         * this is used when showing the Alert state.
         */
        public function get enablePlaceHolder():Boolean
        {
            return _enablePlaceHolder;
        }

        public function set enablePlaceHolder(value:Boolean):void
        {
            _enablePlaceHolder=value;
        }

        [Bindable]
        /**
         *
         * PlaceHolder currentState.
         *
         */
        public function get curretViewState():String
        {
            return _curretViewState;
        }

        public function set curretViewState(value:String):void
        {
            _curretViewState=value;
        }

        [Bindable]
        public function get selectedIndex():int
        {
            return _selectedIndex;
        }

        public function set selectedIndex(value:int):void
        {
            _selectedIndex=value;
        }



        [Bindable]
        /**
         * [PlaceHolderInfo Panel] PlaceHolder's ListAwards
         */
        public function get infoPlaceHolder_listAdwards():Object
        {
            return _infoPlaceHolder_listAdwards;
        }

        public function set infoPlaceHolder_listAdwards(value:Object):void
        {
            _infoPlaceHolder_listAdwards=value;
        }

        [Bindable]
        /**
         * [PlaceHolderInfo Panel] PlaceHolder's Price
         */
        public function get infoPlaceHolder_price():String
        {
            return _infoPlaceHolder_price;
        }

        public function set infoPlaceHolder_price(value:String):void
        {
            _infoPlaceHolder_price=value;
        }

        [Bindable]
        /**
         * [PlaceHolderInfo Panel] PlaceHolder's name
         */
        public function get infoPlaceHolder_labelName():String
        {
            return _infoPlaceHolder_labelName;
        }

        public function set infoPlaceHolder_labelName(value:String):void
        {
            _infoPlaceHolder_labelName=value;
        }

        [Bindable]
        /**
         * PlaceHolder's List
         */

        public function get placeHolderList():ArrayCollection
        {
            return _placeHolderList;
        }

        public function set placeHolderList(value:ArrayCollection):void
        {
            _placeHolderList=value;
        }



        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function PlaceHolderViewPM()
        {
            enablePlaceHolder=true;

        }

        [PostConstruct]
        public function init():void
        {



        }



        //-------------------------------------------
        //
        // Mediator Handlers
        //
        //-------------------------------------------



        [EventHandler(event="UtilityEvent.REFRESH_BINDING", priority="990")]
        /**
         * Refresh bindings.
         *
         *
         */
        public function refreshBindigs(event:UtilityEvent):void
        {
            log.logToConsole(this, "refreshBindigs of the Datagrid..");

            //refresh Datagrid.
            // see PlaceHolder.mxml
            dispatcher.dispatchEvent(new Event("refresh", false));

        }

        [EventHandler(event="UtilityEvent.CHANGE_BUTTON_REGISTRATION", priority="990")]

        /**
         * Sets visible btDeregistration.
         *
         *
         */
        public function changeButton(event:UtilityEvent):void
        {
            log.logToConsole(this, "changing Button.");

            visibleBtRegistration=event.object as Boolean;

        }

        [EventHandler(event="NavigationEvent.VIEW_PLACEHOLDER_MAIN", priority="990")]
        [EventHandler(event="NavigationEvent.VIEW_PLACEHOLDER_ALERT", priority="990")]
        [EventHandler(event="NavigationEvent.VIEW_PLACEHOLDER_DEREGISTER", priority="990")]
        /**
         *Used to change the view state.
         *
         *
         */
        public function setState(event:NavigationEvent):void
        {

            switch (event.type)
            {
                case NavigationEvent.VIEW_PLACEHOLDER_MAIN:

                    gotoMainState();
                    break;

                case NavigationEvent.VIEW_PLACEHOLDER_ALERT:

                    gotoAlertState();
                    break;

                case NavigationEvent.VIEW_PLACEHOLDER_DEREGISTER:
                    gotoDeregisterState();
                    break;

            }
        }



        [EventHandler(event="NavigationEvent.VIEW_PLACEHOLDER_MAIN", priority="990")]
        /**
         *Used <b>ONLY</b> at Startup to refresh placeholderList Binding.
         *
         */
        public function refreshPlaceHolderBinding():void
        {

            this.placeHolderList=applicationModel.placeHolderList;

        }

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------


        /**
         * DataGrid <b>Change</b> event Handler
         *
         * @param selectedIndex
         *
         */
        public function dataGridClicked(selectedIndex:int):void
        {

            //sets info panel
            var temp:PlaceHolderVO=placeHolderList.getItemAt(selectedIndex) as PlaceHolderVO;
            infoPlaceHolder_labelName=temp.tornamentName;
            infoPlaceHolder_price=temp.buyIn;
            infoPlaceHolder_listAdwards="\n" + temp.gameDescription;

            //enable btRegistration
            enableBtRegistration=true;

            //save selected PlaceHolder
            this.selectedIndex=selectedIndex;

            //check if PlaceHolder's already selected
            if (applicationModel.registeredPlaceholder != -1)
            {
                var t:PlaceHolderVO=(applicationModel.placeHolderList.getItemAt(applicationModel.registeredPlaceholder) as PlaceHolderVO);

                var t2:PlaceHolderVO=(applicationModel.placeHolderList.getItemAt(this.selectedIndex) as PlaceHolderVO);
                if (t.tableId == t2.tableId)
                {
                    //change button btRegister
                    visibleBtRegistration=false;

                }
                else
                {
                    visibleBtRegistration=true;
                }

            }
            else
            {
                visibleBtRegistration=true;
            }

            //invokes the controller to write in the ApplicationModel
            //the selected PlaceHolder
            dispatcher.dispatchEvent(new PlaceHolderEvent(PlaceHolderEvent.SET_SELECTED_INDEX, selectedIndex, true));





        }


        /**
         * <b>Handler</b> Button id="bt_Registration"
         * @see PlaceHolderView.mxml
         *
         */
        public function tryRegister():void
        {
            if (applicationModel.registeredPlaceholder != -1)
            {
                enablePlaceHolder=false;

                //alert Confirm
                curretViewState=ApplicationConstants.VIEW_PLACEHOLDER_DEREGISTER;
                //resourceManager.getString('labels','AlertTitle');

                var p:PlaceHolderVO=applicationModel.placeHolderList.getItemAt(applicationModel.registeredPlaceholder) as PlaceHolderVO;
                alertTitle=ResourceManager.getInstance().getString('labels', 'AlertTitle');
                alertMessage="Sei gia' registrato al tavolo n. " + p.tableId + "\n" + "Continuare con l'operazione?" + "\n";

            }
            else
            {
                sendRegistration();
            }

        }


        /**
         * <b>Handler</b> Button id="bt_UnRegister"
         * @see PlaceHolderView.mxml
         *
         */
        public function tryDeregister():void
        {
            log.logToConsole(this, "[tryDeregister()] sendDeregistration...");
            sendDeregistration();

        }

        /**
         * <b>Handler</b> Button id="btConfirm"
         * @see PlaceHolderView.mxml
         *
         */
        public function tryConfirmDeregister():void
        {
            log.logToConsole(this, "[tryConfirmDeregister()] sendDeregistration...");
            sendDeregistration();
            log.logToConsole(this, "[tryConfirmDeregister()] sendRegistration....");
            sendRegistration();
            curretViewState=ApplicationConstants.VIEW_PLACEHOLDER_MAIN;
        }



        /**
         * Goto Main State
         *
         */
        public function gotoMainState():void
        {
            curretViewState=ApplicationConstants.VIEW_PLACEHOLDER_MAIN;
            enablePlaceHolder=true;
        }

        /**
         *	Goto the Alert State
         *
         *
         */
        private function gotoAlertState():void
        {

            enablePlaceHolder=false;
            curretViewState=ApplicationConstants.VIEW_PLACEHOLDER_ALERT;

        }

        /**
         *	Goto the Deregister State
         *
         *
         */
        private function gotoDeregisterState():void
        {

            enablePlaceHolder=false;
            curretViewState=ApplicationConstants.VIEW_PLACEHOLDER_DEREGISTER;

        }


        public function refreshDataGrid():void
        {
            placeHolderList=applicationModel.placeHolderList;


        }

        /**
         *
         * Used to display players_connect/max_players allowed.
         */
        public function labelFunctionPlayers(data:Object, column:DataGridColumn):String
        {
            var s:String="";

            var t:PlaceHolderVO=data as PlaceHolderVO;


            return t.playersNum + "/" + t.playersNum;

        }

        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------

        private function sendRegistration():void
        {

            dispatcher.dispatchEvent(new PlaceHolderEvent(PlaceHolderEvent.TRY_REGISTER_USER));
        }


        private function sendDeregistration():void
        {
            dispatcher.dispatchEvent(new PlaceHolderEvent(PlaceHolderEvent.TRY_UNREGISTER_USER));

        }

    }
}